package com.example.pay.controller;

import com.example.pay.domain.PayBook;
import com.example.pay.service.PayServiceImpl;
import org.hibernate.annotations.Source;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Wu YunXia
 * @Description: 账单
 * @Date: Created in 11:25 2020/12/3
 * @Modefied By:
 */
@RestController
@RequestMapping(value = "/pay")
public class PayController {
    @Resource
    PayServiceImpl payService;

    Map map = new HashMap();

    /**
    * @Date 11:32 2020/12/3
    * @desc: 账单添加
    */
    @RequestMapping(value = "/addPay",method = RequestMethod.POST)
    public Object addPay(HttpServletRequest request, @RequestBody List<PayBook> list){
        String openId = request.getHeader("openId");


        if(openId==null){
            map.put("code",-1);
            map.put("msg","请求头openId为空");
        }else{
            if(list.size()!=0){
                System.out.println("list:"+list.get(0).getPayName());
                int payFor = payService.addPay(openId,list);
                if(payFor>0){
                    map.put("code",200);
                    map.put("msg","添加成功");
                }else{
                    map.put("code",-1);
                    map.put("msg","添加失败");
                }
            }else{
                map.put("code",-1);
                map.put("msg","提交内容不为空");
            }
        }



//        for(int i = 0; i < list.size(); i ++){
//            String name = list.get(i).getPayName();
//            Integer pay = list.get(i).getPay();
//        }
        return map;
    }


    /**
    * @Date 14:15 2020/12/4
    * @desc:
    */
    @RequestMapping("/queryForDate")
    public Object queryForDate(String date){
        List<PayBook> list = payService.queryForDate(date);
        if(list.size()!=0){

            Integer total = 0;
            for(int i = 0; i < list.size(); i ++){
                total += list.get(i).getPay();
            }

            System.out. println("total:"+total);

            map.put("code",200);
            map.put("msg","成功");
            map.put("total",total);
            map.put("list",list);
        }else{
            map.put("code",-1);
            map.put("msg","失败");
        }
        return map;
    }





    /**
    * @Date 15:14 2020/12/3
    * @desc: 查询
    */
    @RequestMapping("/selectAll")
    public Object select(){
        List<PayBook> list = payService.selectAll();
        if(list.size()!=0){
            Integer total = 0;
            for(int i = 0; i < list.size(); i ++){
                total += list.get(i).getPay();
            }

            System.out.println("total:"+total);

            map.put("code",200);
            map.put("msg","成功");
            map.put("total",total);
            map.put("list",list);
        }else{
            map.put("code",-1);
            map.put("msg","失败");
        }
        return map;
    }

}
